#!/usr/bin/perl -w # for syntax highlighting
# CSS-V1

# Take all the args and update existing section details
# given section and sec_* reader_* and publisher_* values...
# redirect to page in the LP parameter.
use gri_secmgr qw(get_cookie_value redirect_header);
use gri_user_alert_config;
use gri_database;

sub mm_form_docrepos_modify_section_process {
	my $str="";

	my $db_dir=$::GRI_FRONTEND->get_global_directory("database");
	my $DB=new gri_database("$db_dir/gri.db");
	
	my $section=$::PARAM{section};
	my $sec_parent=$::PARAM{parent};
	my $sec_title=$::PARAM{title};
	my $sec_description=$::PARAM{description};
	my $sec_attributes=$::PARAM{attributes};

	my @R=(); my @P=();
	for my $ckey (keys %::PARAM) {
		if($ckey =~ /^reader_(.*)/) {
			push @R,$1;
		}
		if($ckey =~ /^publisher_(.*)/) {
			push @P,$1;
		}
	}
	my $sec_publishers=join(",",@P);
	my $sec_readers=join(",",@R);

	$sec_parent="" if !defined($sec_parent);
	$sec_readers="" if !defined($sec_readers);
	$sec_publishers="" if !defined($sec_publishers);

	# We have all the information so update the section details ...
	my ($rc,$msg)=$DB->modify_section(section=>$section,
		parent=>$sec_parent,
		title=>$sec_title,
		description=>$sec_description,
		reader_groups=>$sec_readers,
		attributes=>$sec_attributes,
		publisher_groups=>$sec_publishers);

	if($rc!=1) {
		my $serror="Error Updating Existing Section";
		my $error=$msg;
		my $link=link_to_str("","","show_generic_error","?serror=".string_to_hex($serror).",error=".string_to_hex($error));
		if(!exists($::PARAM{lp})) {
			# We need to encode the current data for repopulation into the form!
			my $s="";
			$s.="section=".string_to_hex($section);
			$s.="::sec_title=".string_to_hex($sec_title);
			$s.="::sec_description=".string_to_hex($sec_description);
			$s.="::sec_attributes=".string_to_hex($sec_attributes);
			$s.="::sec_parent=".string_to_hex($sec_parent);
			$s.="::sec_readers=".string_to_hex($sec_readers);
			$s.="::sec_publishers=".string_to_hex($sec_publishers);
			$link.=",previouspage=form_docrepos_modify_do::$s";
		}
		redirect_header(path => $link);
		exit;
	}

	my $link=link_to_str("","","/admin_page","");
	redirect_header(path => $link);
	exit;
}

# vim: ts=4:sw=4
